const express = require('express')
const router = express.Router()
const { ApplyModel } = require('../model/index')

router.post('/applyit', (req, res, next) => {
  const { username, cart, address, date, mon } = req.body
  console.log(mon)
  new ApplyModel({ username, cart, address, date, sure:false, mon }).save()
    .then(resp => {
      res.json({
        code: 200,
        date: resp
      })
    }).catch(err => {
      res.json({
        code: 400,
        date: err
      })
    })
})

// router.post('/getapply', (req, res, next) => {
//   const { username } = req.body
//   ApplyModel.find({ username }, (err, data) => {
//     if (err) {
//       res.json({
//         code: 500,
//         data: err
//       })
//     } else {
//       res.json({
//         code: 200,
//         data: data
//       })
//     }
//   })
// })
router.post('/getmyorder1', (req, res, next) => {
  const { username } = req.body
  ApplyModel.find({ username, sure: true }, (err, date) => {
    if (err) {
      res.json({
        code: 500,
        data: err
      })
    } else {
      res.json({
        code: 200,
        data: date
      })
    }
  })
})

router.post('/getmyorder2', (req, res, next) => {
  const { username } = req.body
  ApplyModel.find({ username, sure: false }, (err, date) => {
    if (err) {
      res.json({
        code: 500,
        data: err
      })
    } else {
      res.json({
        code: 200,
        data: date
      })
    }
  })
})


router.post('/sureorder', (req, res, next) => {
  const { id } = req.body
  console.log(id)
  ApplyModel.findOneAndUpdate({ _id: id }, { $set: {sure: true} }, {}, (err, data) => {
    if (err) {
      res.json({
        code: 500,
        data: err
      })
    } else {
      res.json({
        code: 200,
        data: data
      })
    }
  })
})

module.exports = router